接下來我們要來製作我們的分數機制。

Photo by Jeswin Thomas on Unsplash
let score = 0;
function create(){
  ...
  // 加上分數文字在場景內
  scoreText = this.add.text(470, 35, "B0", {
    fontSize: "40px",
    fill: "#0000aa",
  });
}
function update(){
  ...
  
  grounds.getChildren().forEach((el) => {
    if (el.y < 0) {
      el.destroy();
      // 加上分數機制
      score++;
      scoreText.setText("B" + score);
      ground = grounds
        .create(
          Phaser.Math.Between(0, 600),
          Phaser.Math.Between(1200, 1250),
          "ground"
        )
        .setScale(0.5);
      ground.body.immovable = true;
      ground.body.checkCollision = groundCollision;
      ground.isHeal = false;
    }
  });
}
再來把之前給定的樓梯速度改變成變數,並且讓他因為分數越來越快。
// 樓梯漂浮速度
let speed = 100;
function update(){
  ...
  
  grounds
    .getChildren()
    .forEach((el) => el.setVelocityY(speed * -1 - score * 5));
}
太棒了~我們的遊戲已經可以完整的運作了!明天就來加上最後的機制!
加油!加油!下樓梯快到尾聲了~!
Phaser Game 2020鐵人賽